package top.withwings.bonus.bonus.service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;
import top.withwings.bonus.bonus.pojo.BonusForm;
import top.withwings.bonus.bonus.pojo.OnSaleForm;
import top.withwings.bonus.bonus.pojo.bonus.rule.*;
import top.withwings.bonus.bonus.repo.BonusRuleRepo;
import top.withwings.bonus.bonus.repo.ExpirationCloseAttachmentRepo;
import top.withwings.bonus.bonus.repo.HighPriceAttachmentRepo;
import top.withwings.bonus.bonus.repo.OnSaleFormRepo;
import top.withwings.bonus.common.pojo.PageParam;

import java.util.List;

@Service
public class RuleService {

    @Autowired
    private BonusRuleRepo bonusRuleRepo;

    @Autowired
    private RuleAttachmentService ruleAttachmentService;


    @Autowired
    private OnSaleFormService onSaleFormService;

    @Autowired
    private HighPriceAttachmentRepo highPriceAttachmentRepo;

    @Autowired
    private ExpirationCloseAttachmentRepo expirationCloseAttachmentRepo;

    @Autowired
    private OnSaleFormRepo onSaleFormRepo;


    public List<BonusRule> listRules() {
        return bonusRuleRepo.listAll();
    }


    public GrossAmountBonusRule findGrossAmountRule() {
        for (BonusRule rule : listRules()) {
            if (rule.getType().equalsIgnoreCase(GrossAmountBonusRule.TYPE)) {
                return (GrossAmountBonusRule) rule;
            }
        }
        throw new IllegalStateException("NO GrossAmountBonusRule DEFINED ");
    }


    public OnSaleBonusRule findOnSaleRule(BonusForm bonusForm) {

        String batchNo = bonusForm.getBatchNo();
        if (StringUtils.isEmpty(batchNo)) {
            return null;//不填批次不享受任何促销提成
        }

        int goodId = bonusForm.getGood().getId();
        List<OnSaleForm> onSaleForms = onSaleFormService.findByGoodAndTime(new PageParam(0, Integer.MAX_VALUE), goodId, bonusForm.getEffectiveDate()).getData();

        for (OnSaleForm onSaleForm : onSaleForms) {
            if (onSaleForm.getBatchNos().contains(bonusForm.getBatchNo())) {
                return onSaleForm.getRule();
            }
        }

        return null;

    }


    public HighPriceBonusRule findHighPriceRule(int id) {
        return ruleAttachmentService.findHighPriceRule(id);
    }

    public ExpirationCloseBonusRule findCloseExpirationRule(int id) {
        return ruleAttachmentService.findExpirationCloseRule(id);
    }

    public ExpirationCloseBonusRule findCloseExpirationRule(BonusForm bonusForm) {
        return ruleAttachmentService.findExpirationCloseRule(bonusForm.getGood().getId());
    }

    public HighPriceBonusRule findHighPriceRule(BonusForm bonusForm) {
        return ruleAttachmentService.findHighPriceRule(bonusForm.getGood().getId());
    }

    @Transactional
    public void create(BonusRule bonusRule) {
        bonusRuleRepo.create(bonusRule);
    }

    @Transactional
    public void delete(int bonusRuleId) {
        BonusRule rule = bonusRuleRepo.findById(bonusRuleId);
        checkDelete(rule);
        bonusRuleRepo.delete(bonusRuleId);
    }


    @Transactional
    public void update(BonusRule bonusRule) {
        bonusRuleRepo.update(bonusRule);
    }


    @Transactional(readOnly = true)
    private void checkDelete(BonusRule rule) {
        if (!(rule instanceof SingleBonusRule)) {
            throw new IllegalArgumentException("只能删除单项提成规则，总提成规则不能删除");
        }

        if (rule instanceof HighPriceBonusRule && highPriceAttachmentRepo.hasAttachment(rule.getId())) {
            throw new IllegalStateException("该高价提规则已经被商品使用，不能删除");
        }

        if (rule instanceof ExpirationCloseBonusRule && expirationCloseAttachmentRepo.hasAttachment(rule.getId())) {
            throw new IllegalStateException("该临保提规则已经被商品使用，不能删除");
        }

        if (rule instanceof OnSaleBonusRule && onSaleFormRepo.countByRule(rule.getId()) > 0) {
            throw new IllegalStateException("该促销提规则已经被促销单使用，不能删除");
        }
        // check Pass
    }



}